// $codepro.audit.disable
package edu.gatech.team404.junit;

import java.util.List;

import edu.gatech.team404.Account;
import edu.gatech.team404.AccountStatus;
import edu.gatech.team404.AccountType;
import edu.gatech.team404.dal.AccountDAL;
import edu.gatech.team404.tool.Log;
import junit.framework.TestCase;

public class AccountDALTest extends TestCase
{
	Account admin = new Account ("admin", "admin", "Administrator", "admin@gatech.edu", AccountType.ADMIN);
	Account instructor = new Account ("instructor", "instructor", "Instructor", "instructor@gatech.edu", AccountType.INSTRUCTOR);
	Account student = new Account ("student", "student", "Student", "student@gatech.edu", AccountType.STUDENT);
	
	protected void setUp() throws Exception 
	{
		super.setUp();
	}
	
	public void testAddAccount()
	{
		AccountDAL dal = new AccountDAL();
		
		dal.addAccount(admin);
		dal.addAccount(instructor);
		dal.addAccount(student);
		
		assertEquals(admin.getUsername(), dal.getAccount("admin").getUsername());
		assertEquals(instructor.getUsername(), dal.getAccount("instructor").getUsername());
		assertEquals(student.getUsername(), dal.getAccount("student").getUsername());
	}
	
	public void testGetAccount()
	{
		AccountDAL dal = new AccountDAL();
		Account acct = dal.getAccount("admin");
		
		assertEquals(admin.getUsername(), acct.getUsername());
		assertEquals(admin.getPassword(), acct.getPassword());
		assertEquals(admin.getName(), acct.getName());
		assertEquals(admin.getEmail(), acct.getEmail());
		assertEquals(admin.getType().toString(), acct.getType().toString());
		assertEquals(admin.getStatus().toString(), acct.getStatus().toString());
	}
	
	public void testGetAllAccounts()
	{
		AccountDAL dal = new AccountDAL();
		List<Account> accts = dal.getAllAccounts();
		
		/* May fail since we don't know how the database looks like */
		assertEquals(admin.getUsername(), accts.get(0).getUsername());
		assertEquals(instructor.getUsername(), accts.get(1).getUsername());
		assertEquals(student.getUsername(), accts.get(2).getUsername());
	}
	
	public void testUpdateAccount()
	{
		Account newAcct = new Account ("instructor", "", "CS2340 Instructor", "", AccountType.INSTRUCTOR);
		AccountDAL dal = new AccountDAL();
		
		dal.updateAccount(newAcct);
		
		assertEquals(newAcct.getName(), dal.getAccount(newAcct.getUsername()).getName());
	}
	
	public void testRemoveAccount()
	{
		Account del = new Account ("delete", "delete", "delete", "delete@gatech.edu", AccountType.UNKNOWN);
		AccountDAL dal = new AccountDAL();
		
		dal.addAccount(del);
		
		assertEquals(del.getUsername(), dal.getAccount(del.getUsername()).getUsername());
		
		dal.removeAccount(del.getUsername());
		
		Log.outputVerbose();
		assertNull (dal.getAccount(del.getUsername()));
	}
	
	public void testSuspend()
	{
		AccountDAL dal = new AccountDAL();
		dal.suspend(student.getUsername());
		Account acct = dal.getAccount(student.getUsername());
		
		assertEquals(AccountStatus.SUSPENDED.toString(), acct.getStatus().toString());
	}
	
	public void testUnsuspend()
	{
		AccountDAL dal = new AccountDAL();
		dal.unsuspend(student.getUsername());
		Account acct = dal.getAccount(student.getUsername());
		
		assertEquals(AccountStatus.ACTIVE.toString(), acct.getStatus().toString());	
	}
}
